import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

const Login = () => import("../views/Login.vue");
const Index = () => import("../views/Index.vue");
const List = () => import("../views/Crud/List.vue");
const Edit = () => import("../views/Crud/Edit.vue");

const routes: RouteRecordRaw[] = [
  { path: "/login", component: Login, meta: { isPublic: true } },
  {
    path: "/",
    component: Index,
    children: [
      { path: "/:resource/list", component: List, props: true },
      { path: "/:resource/list/:status", component: List, props: true },
      { path: "/:resource/create", component: Edit, props: true },
      { path: "/:resource/edit/:id", component: Edit, props: true },
    ],
    redirect: "/orders/list/1",
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
});

router.beforeEach((to) => {
  if (!to.meta.isPublic && !localStorage.adminToken) {
    return "/login";
  }
});

export { router };
